<?php 
	session_start();
	include("dbconfig.php");
	
	$verclientefactura 	= 	$_SESSION['caweb']['verclientefactura'];
	$versucursalfactura = 	$_SESSION['caweb']['versucursalfactura'];
	$vernumerofactura 	=	$_SESSION['caweb']['vernumerofactura'];

	$condicion="";
	if ($verclientefactura <> '0') $condicion.=" AND cliente = '$verclientefactura'";
	if ($versucursalfactura <> '0') $condicion.=" AND sucursal = '$versucursalfactura'";
	if ($vernumerofactura <> '') $condicion.=" AND factura = '$vernumerofactura'";
	

	$page = $_REQUEST['page']; 

	// get how many rows we want to have into the grid - rowNum parameter in the grid 
	$limit = $_REQUEST['rows']; 

	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel 
	$sidx = $_REQUEST['sidx']; 
	
	// sorting order - at first time sortorder 
	$sord = $_REQUEST['sord']; 

	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1; 

	// connect to the MySQL database server 
	$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error()); 

	// select the database 
	mysql_select_db($database) or die("Error connecting to db."); 

	// calculate the number of rows for the query. We need this for paging the result 
	$result = mysql_query("SELECT COUNT(*) AS count FROM cabeceras_fac WHERE factura > 0 $condicion "); 
	$row = mysql_fetch_array($result,MYSQL_ASSOC); 
	$count = $row['count']; 

	// calculate the total pages for the query 
	if( $count > 0 ) { 
				  $total_pages = ceil($count/$limit); 
	} else { 
				  $total_pages = 0; 
	} 

	// if for some reasons the requested page is greater than the total 
	// set the requested page to total page 
	if ($page > $total_pages) $page=$total_pages;

	// calculate the starting position of the rows 
	$start = $limit*$page - $limit;

	// if for some reasons start position is negative set it to 0 
	// typical case is that the user type 0 for the requested page 
	if($start <0) $start = 0; 

	// the actual query for the grid data
	 
	$SQL = "SELECT * FROM cabeceras_fac WHERE factura > 0 $condicion ORDER BY $sidx $sord LIMIT $start , $limit"; 
	$result = mysql_query( $SQL ) or die("Error en cabeceras.".mysql_error()); 

	// we should set the appropriate header information
	if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
				  header("Content-type: application/xhtml+xml;charset=utf-8"); 
	} else {
			  header("Content-type: text/xml;charset=utf-8");
	}
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<rows>";
	echo "<page>".$page."</page>";
	echo "<total>".$total_pages."</total>";
	echo "<records>".$count."</records>";

	while($row = mysql_fetch_array($result)) {
		echo "<row id='". $row[facturasistema]."'>";
		echo "<cell></cell>";
		echo "<cell>". $row[factura]."</cell>";
		echo "<cell>". $row[tipo]."</cell>";
		echo "<cell>". $row[base]."</cell>";
		echo "<cell>". $row[iva]."</cell>";
		echo "<cell>". $row[ivaespecialfactura]."</cell>";
		echo "<cell>". $row[descuentobaseiva]."</cell>";
		echo "<cell>". $row[total]."</cell>";
		echo "<cell>". $row[cliente]."</cell>";
		echo "<cell>". utf8_encode(htmlspecialchars($row[nombre]))."</cell>";
		echo "<cell>". $row[fechafactura]."</cell>";
		echo "<cell>". $row[estado]."</cell>";
		echo "<cell>". $row[estadocartera]."</cell>";
		echo "<cell>". $row[juridico]."</cell>";
		echo "<cell>". $row[soloespecial]."</cell>";
		echo "<cell>". $row[ivafactura]."</cell>";
		echo "<cell>". utf8_encode(htmlspecialchars($row[referencia]))."</cell>";
		echo "<cell>". $row[sucursal]."</cell>";
		echo "<cell>". utf8_encode(htmlspecialchars($row[observacion]))."</cell>";		
		echo "</row>";
	}
	echo "</rows>"; 
?>
